package com.KKd.controller;

import com.KKd.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author 14587
 * @date 2020/11/12 14:49
 */
@RequestMapping("/user")
@Controller
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    //展示用户信息
    @RequestMapping("/show")
    public String showUser(){
        return "user";
    }
    //前往登录页面
    @RequestMapping("/toLogin")
    public String toLogin(HttpSession session){
        if (session.getAttribute("user")!=null){
            return "redirect:/page/showPage";
        }
        return "login";
    }
    //处理登录程序
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession httpSession){
                if (userService.logIn(username, password)){
                    //这里不能用Model，因为model相当于request域，换了请求之后就不存在了
                    Map<String, Object> user = userService.showUser();
                    httpSession.setAttribute("user", user);
                }
        return "redirect:/page/showPage";
    }
}
